/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package client.bean;

import client.dal.CustomerDAL;
import client.dal.FeedBackDAL;
import client.dal.RecruitmentDAL;
import client.entity.Account;
import client.entity.Customer;
import client.entity.FeedbackSystem;
import client.entity.Recruitment;
import java.util.ArrayList;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author mrlucky
 */
@ManagedBean
@SessionScoped
public class CustomerProfile {

    private Customer cus;
    private ArrayList<Recruitment> listRec;
    private int accId;
    private FeedbackSystem fs;

    public FeedbackSystem getFs() {
        if (fs == null) {
            fs = new FeedbackSystem();
        }
        return fs;
    }

    public void setFs(FeedbackSystem fs) {
        this.fs = fs;
    }

    public int getAccId() {
        return accId;
    }

    public void setAccId(int accId) {
        this.accId = accId;
    }

    public Customer getCus() {
        if (cus == null) {
            Account ac = (Account) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("account");
            accId = ac.getId();
            cus = new CustomerDAL().loadCustomerByAccountID(accId);
            if (cus.getLogo() == null) {
                cus.setLogo("LogoW.png");
            }
            listRec = new RecruitmentDAL().loadRecByCustomerID(cus.getId());
        }
        return cus;
    }

    public void setCus(Customer cus) {
        this.cus = cus;
    }

    public ArrayList<Recruitment> getListRec() {
        if (listRec == null) {
            listRec = new ArrayList<Recruitment>();
        }
        return listRec;
    }

    public void setListRec(ArrayList<Recruitment> listRec) {
        this.listRec = listRec;
    }

    /**
     * Creates a new instance of CustomerProfile
     */
    public CustomerProfile() {
    }

    public String getParam(String param) {

        Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        return params.get(param);

    }
    
    public String editRec(int recID) {
        HttpSession ss = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        ss.removeAttribute("customerEditProfile");
        return "customerProfileEdit.xhtml?cvID=" + recID + "&faces-redirect=true";
    }
    
    public String editProfile() {
        return "customerEditInfo.xhtml?faces-redirect=true";
    }

    public String deleteRec(Recruitment rec) {
        new RecruitmentDAL().deleteRec(rec);
        HttpSession ss = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        ss.removeAttribute("customerProfile");
        return "customerProfile.xhtml?faces-redirect=true";
    }

    public String sendFeedBack() {
        fs.setCustomer(cus);
        new FeedBackDAL().insertFeedBack(fs);
        HttpSession ss = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        ss.removeAttribute("customerProfile");
        return "customerProfile.xhtml?faces-redirect=true";
    }
}
